package com.tom.architect.party3.day32.simple1;

import android.Manifest;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.jakewharton.rxbinding2.view.RxView;
import com.jakewharton.rxbinding2.widget.RxTextView;
import com.tbruyelle.rxpermissions3.RxPermissions;
import com.tom.architect.R;

import androidx.appcompat.app.AppCompatActivity;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.BiFunction;
import io.reactivex.functions.Consumer;


// compile 'com.jakewharton.rxbinding2:rxbinding:2.0.0'
public class MainActivity3201 extends AppCompatActivity {
    private EditText mUserNameEt, mUserPwdEt;
    private View mClearContent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main32);
        mUserNameEt = findViewById(R.id.user_name_et);
        mUserPwdEt = findViewById(R.id.user_password_et);
        mClearContent = findViewById(R.id.clear_content);

        // 6.0 权限。
        RxPermissions rxPermissions = new RxPermissions(this);
//        rxPermissions.request(Manifest.permission.CAMERA)

    }


    // rxbinding
    private void rxbindingMethod() {
        // 响应式编程常见的使用场景。
        // 1.清空按钮是否可见
        RxTextView.textChanges(mUserNameEt).subscribe(new Consumer<CharSequence>() {
            @Override
            public void accept(CharSequence text) throws Exception {
                // 有输入文字，显示删除按钮。
                mClearContent.setVisibility(TextUtils.isEmpty(text) ? View.VISIBLE : View.GONE);
            }
        });

        // 2.联合使用，合并
        Observable<CharSequence> userNameObservable = RxTextView.textChanges(mUserNameEt);
        Observable<CharSequence> userPwdObservable = RxTextView.textChanges(mUserPwdEt);
        Observable.combineLatest(userNameObservable, userPwdObservable, new BiFunction<CharSequence, CharSequence, Object>() {
            @Override
            public Object apply(CharSequence name, CharSequence pwd) throws Exception {
                // 设置按钮是否可用（或改变背景颜色）
                mClearContent.setEnabled(!TextUtils.isEmpty(name) && !TextUtils.isEmpty(pwd));
                return null;
            }
        });
        // 3.防止重复点击
        RxView.clicks(mClearContent).debounce(300, TimeUnit.MILLISECONDS)
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {

                    }
                });
        // 4.控制操作的时间间隔。防止太频繁请求网络
        RxTextView.textChanges(mUserNameEt).debounce(1200, TimeUnit.MILLISECONDS)
                .subscribe(new Consumer<CharSequence>() {
                    @Override
                    public void accept(CharSequence charSequence) throws Exception {

                    }
                });

        // 5.接口轮询。
        Observable.interval(2, 2, TimeUnit.SECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) throws Exception {
                        // 处理UI更新。
                    }
                });
        // 6.延时操作，2s后操作。
        Observable.timer(2, TimeUnit.SECONDS)
                .subscribe(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) throws Exception {

                    }
                });
    }
}
